package gui.menu.edit;

import data.DataVector;
import gui.commands.CommandManager;
import gui.menu.classDiagram.AddClassForm;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.RichTooltip;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

/**
 * User: Szymon Kudzia
 * Date: 10.05.13
 * Time: 14:17
 */
public class UndoItem extends JCommandButton implements ActionListener, Observer {
	ResizableIcon normalIcon;
	ResizableIcon disabledIcon;

	private AddClassForm form;

	public UndoItem() {
		super(ImageWrapperResizableIcon.getIcon(
				ClassLoader.getSystemClassLoader().getResource("icons/undo.png"),
				new Dimension(16, 16))
		);

		this.setActionRichTooltip(new RichTooltip("Undo", "Undo last action"));
		addActionListener(this);

		normalIcon = ImageWrapperResizableIcon.getIcon(
				ClassLoader.getSystemClassLoader().getResource("icons/undo.png"),
				new Dimension(16, 16)
		);

		disabledIcon = ImageWrapperResizableIcon.getIcon(
				ClassLoader.getSystemClassLoader().getResource("icons/undoDisabled.png"),
				new Dimension(16, 16)
		);

		DataVector.getInstance().addObserver(this);
		update(null, null);
	}

	public void actionPerformed(ActionEvent e) {
		if (DataVector.getInstance().getActiveStateDiagram() == null)
			CommandManager.getInstance().undoClassDiagramCommand();
		else
			CommandManager.getInstance().undoStateDiagramCommand();
	}

	@Override
	public void update(Observable observable, Object o) {
		if (DataVector.getInstance().getActiveStateDiagram() == null) {
			if (CommandManager.getInstance().canUndoClassDragramCommand())
				this.setIcon(normalIcon);
			else
				this.setIcon(disabledIcon);
		} else {
			if (CommandManager.getInstance().canUndoStateDragramCommand())
				this.setIcon(normalIcon);
			else
				this.setIcon(disabledIcon);
		}

		repaint();
		doLayout();
	}
}
